<?php
/**
 * ClassName:excel导入接口
 * @auditor:leijunxiang
 * @time 2025/4/12 16:50
 */

namespace app\controller\freightForward;

use app\BaseController;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\Request;
use app\tool\word\ExcelInputHandle;
use think\facade\Log;

class ExcelController extends BaseController
{
    /**
     * 导入数据
     * @return void
     */
    public function inputData()
    {
        $response = app(ResponseAgreement::class);
        $file = $this->request->file('file');
        if (empty($file)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请上传文件！");
            return;
        }
        $forwardId = $this->request->post('forwardId');
        $forwardId = isset($forwardId) ? $forwardId : '';
        if (empty($forwardId)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入货代id！");
            return;
        }
        $startCoord = $this->request->post('startName');
        $startCoord = isset($startCoord) ? $startCoord : '';
        if (empty($startCoord)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入开始标识！");
            return;
        }
        $lineEnd = $this->request->post('lineEnd');
        $lineEnd = isset($lineEnd) ? $lineEnd : '';
        if (empty($lineEnd)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入行结束位移！");
            return;
        }
        $input = app(ExcelInputHandle::class);
        //开始导入(文件,货代id,渠道id,开始标识,行结束位移)
        $flag = $input->startInput($file, $forwardId, $startCoord, $lineEnd);
        if (!$flag) {
            $response->setErrCode($input->getErrCode());
            $response->setErrMsg($input->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("导入成功");
        }
    }
}